<?php
/**
 * Created by PhpStorm.
 * User: lyh
 * Date: 2016/10/24
 * Time: 16:58
 */
use OSS\OssClient;
use OSS\Core\OssException;

require_once "./ThinkPHP/Library/Vendor/OSS/autoload.php";
function checkorderstatus($ordid){
    $Ord=M('client_book');
    $ordstatus=$Ord->where(array('ordid'=>$ordid))->getField('ordstatus');
    if($ordstatus==1){
        return true;
    }else{
        return false;
    }
}

//处理订单函数
//更新订单状态，写入订单支付后返回的数据
function orderhandle($parameter){
    $ordid=$parameter['out_trade_no'];
    $data['payment_trade_no']      =$parameter['trade_no'];
    $data['payment_trade_status']  =$parameter['trade_status'];
    $data['payment_notify_id']     =$parameter['notify_id'];
    $data['payment_notify_time']   =$parameter['notify_time'];
    $data['payment_buyer_email']   =$parameter['buyer_email'];
    $data['ordstatus']             =1;
    $data['finishtime']=date('YmdHi',time());
    $Ord=M('client_book');
    $Ord->where(array('ordid'=>$ordid))->save($data);
}

/**
 * 图片上传
 * @param $name
 * @param $flag
 * @return array|void
 */
function upload($name,$flag){
    $setting = array (
        'maxSize' => 5 * 1024 * 1024,//文件大小
        'rootPath' => 'http://image.chinaola.net/',
        'saveName' =>md5($name.time()) ,
        'savePath' => 'aliyun/postimage/',    //保存路径
        'driver' => 'Aliyun',
        'driverConfig' => array (
            'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
            'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
            'domain' => OSS,        //
            'Bucket' => 'ola',         //Bucket
            'Content-Type' => 'image/jpeg',
            'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
        ),
    );
//
    $config = array (
        'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
        'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
        'domain' => OSS,        //
        'Bucket' => 'ola',         //Bucket
        'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
        'saveName'=>md5($name.time()),
    );

    try {
        $ossClient = new OssClient($config['AccessKeyId'],$config['AccessKeySecret'],$config['Endpoint']);
    } catch (OssException $e) {

    }

    $randstr = rand(1,100000);
    //获取图片后缀名
    $hc_big = explode('.',$_FILES['image']['name']);
    $big_aaa = $hc_big[1];
    //图片保存路径
    $path = $setting['savePath'].$flag.'/' .$setting['saveName'].$randstr.'.'.$big_aaa;
    try{
        $ossClient->uploadFile($config['Bucket'], $path , $_FILES["image"]["tmp_name"]);
    } catch(OssException $e) {
        return;
    }
    //存储图片路径
    $filepath = $setting['rootPath']. $path;
    return $filepath;
}

/**
 * 医生审核图片上传
 * @return array|void
 */
function uploadDoctor(){
    $setting = array (
        'maxSize' => 5 * 1024 * 1024,//文件大小
        'rootPath' => 'http://image.chinaola.net/',
        'saveName' =>md5(time()) ,
        'savePath' => 'aliyun/doctorimg/',    //保存路径
        'driver' => 'Aliyun',
        'driverConfig' => array (
            'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
            'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
            'domain' => OSS,        //
            'Bucket' => 'ola',         //Bucket
            'Content-Type' => 'image/jpeg',
            'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
        ),
    );
//
    $config = array (
        'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
        'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
        'domain' => OSS,        //
        'Bucket' => 'ola',         //Bucket
        'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
        'saveName'=>md5(time()),
    );

    try {
        $ossClient = new OssClient($config['AccessKeyId'],$config['AccessKeySecret'],$config['Endpoint']);
    } catch (OssException $e) {

    }
    //循环上传图片 保存地址到path
    $path = array();
    foreach ($_FILES as $k=>$file){
        if(empty($file['tmp_name'])){continue;}
        //技能图片上传为数组形式
        if($k == 'img_skill'){
            for($i=0;$i<count($file['name']);$i++){
                if(empty($file['tmp_name'][$i])){continue;}
                $randstr = rand(1,100000);
                //获取图片后缀名
                $hc_big = explode('.',$file['name'][$i]);
                $big_aaa = $hc_big[1];
                //图片保存路径
                $path[$k][$i] = $setting['savePath'].$setting['saveName'].$randstr.'.'.$big_aaa;
                try{
                    $ossClient->uploadFile($config['Bucket'], $path[$k][$i] , $file["tmp_name"][$i]);
                } catch(OssException $e) {
                    $path['error'];
                }
            }
        }else{
            $randstr = rand(1,100000);
            //获取图片后缀名
            $hc_big = explode('.',$file['name']);
            $big_aaa = $hc_big[1];
            //图片保存路径
            $path[$k] = $setting['savePath'].$setting['saveName'].$randstr.'.'.$big_aaa;
            try{
                $ossClient->uploadFile($config['Bucket'], $path[$k] , $file["tmp_name"]);
            } catch(OssException $e) {
                $path['error'];
            }
        }
    }
    //存储图片路径
    foreach ($path as $k=>&$row){
        if($k == 'img_skill'){
            foreach ($row as &$item){
                $item = $setting['rootPath'] . $item;
            }
        }else{
            $row = $setting['rootPath'] . $row;
        }
    }
    return $path;
}

/**
 * 获取错误信息
 * @param \Think\Model $model 模型.
 * @return string
 */
function get_errors(\Think\Model $model) {
    $errors = $model->getError();
    if (is_string($errors)) {
        return $errors;
    }

    $html = '<ol>';
    foreach ($errors as $error) {
        $html .= '<li>' . $error . '</li>';
    }
    $html .= '</ol>';
    return $html;
}


